﻿using System.Diagnostics;

namespace DimensionsHelper.LanguageService.Syntax;

/// <summary>
///     codes后缀在脚本中视作一个单独的字段，命名成Codes。此字段应当声明在字段中，
///     在字段外或子字段列表中声明的Codes不冲突。
/// </summary>
public sealed class MetadataCodes : MetadataField
{
    private MetadataFieldReference? _fieldReference;

    internal MetadataCodes(SyntaxToken declareKeywordToken, SyntaxNode? parent) :
        base(NodeConverter.ToIdentifier(declareKeywordToken),
            declareKeywordToken,
            parent)
    {
    }

    public MetadataFieldReference? FieldReference => _fieldReference;


    public void Update(MetadataFieldReference fieldReference)
    {
        SetChildNullableField(ref _fieldReference, fieldReference);
    }


    public override void AcceptVisitor<T>(SyntaxNodeVisitor<T> visitor)
    {
        Accept(visitor, DeclareKeywordToken, FieldReference);
        AcceptList(visitor, Categories);
    }
}


public static partial class NodeFactory
{
    public static MetadataCodes CreateMetadataCodes(SyntaxNode? parent, SyntaxToken codesKeywordToken,
        MetadataCategories? categories)
    {
        Debug.Assert(codesKeywordToken.IsNode(SyntaxKinds.CodesKeywordToken));
        MetadataCodes codes = new(codesKeywordToken, parent);
        if (categories != null)
        {
            codes.Update(categories);
        }

        return codes;
    }
}